﻿using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Eamon.MeituanDotnetSDK.utils
{
    public class JsonUtil
    {
        public JsonUtil()
        {
        }
        private static JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            //PropertyNameCaseInsensitive = true,
            Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        };
        public static string ToJson(object obj)
        {
            var result = JsonSerializer.Serialize(obj, jsonSerializerOptions);
            return result;
        }

        public static T JsonToObject<T>(string str, Type t)
        {
            return (T)JsonSerializer.Deserialize(str, t, jsonSerializerOptions);
        }

        public static T FromJson<T>(string response)
        {
            return JsonSerializer.Deserialize<T>(response, jsonSerializerOptions);
        }
    }
}
